//
//  HWSegmentOneView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/21.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWSegmentOneView.h"

@interface HWSegmentOneView()
@property (nonatomic, strong)NSMutableArray *btnArr;
@end

@implementation HWSegmentOneView
- (NSMutableArray *)btnArr
{
    if (!_btnArr)
    {
        _btnArr = [[NSMutableArray alloc]init];
    }
    return _btnArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(16*kUIScaleBasePlus,34*kUIScaleBasePlus,343*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        lineView.backgroundColor = LCHexColor(0xEBE5E1);
        [self addSubview:lineView];
    }
    
    return self;
}

#pragma mark UI
- (void)creatUI:(NSArray *)arr
{
    CGFloat width = 0;
    if (arr.count == 1)
    {
        width = 50*kUIScaleBasePlus;
    }
    else
    {
        width = (SCREEN_WIDTH-32*kUIScaleBasePlus-(arr.count-1)*10*kUIScaleBasePlus)/arr.count;
    }
    
    CGFloat height = 20*kUIScaleBasePlus;
    CGFloat margin = 10*kUIScaleBasePlus;
    
    for (int i=0;i<arr.count;i++)
    {
        UIButton *btn = [HWUtilties creatButtonWithFrame:CGRectMake(16*kUIScaleBasePlus+(width+margin)*i,8*kUIScaleBasePlus,width,height)
                                            textAligment:NSTextAlignmentCenter
                                                    text:arr[i]
                                                textClor:LCHexColor(0xF54337)
                                               buttonBGC:[UIColor clearColor]
                                                textFont:12*kUIScaleBasePlus
                                                  target:self
                                                  method:@selector(segmentClick:)];
        btn.tag = i;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 4;
        btn.layer.borderColor = LCHexColor(0xF54337).CGColor;
        btn.layer.borderWidth = 1;
        
        if (i==0)
        {
            [btn setBackgroundColor:LCHexColor(0xF54337)];
            [btn setTitleColor:LCHexColor(0xFFFFFF) forState:UIControlStateNormal];
        }
        [self.btnArr addObject:btn];
        
        [self addSubview:btn];
    }
}

- (void)segmentClick:(UIButton *)sender
{
    
    [self.btnArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *btn = obj;
        if (idx != sender.tag)
        {
            [btn setBackgroundColor:[UIColor clearColor]];
            [btn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
        }
        else
        {
            [btn setBackgroundColor:LCHexColor(0xF54337)];
            [btn setTitleColor:LCHexColor(0xFFFFFF) forState:UIControlStateNormal];
        }
    }];
    
}

- (void)getSegmentTitle:(NSArray *)arr
{
    [self creatUI:arr];
}

@end
